package com.mol.market.ui_v4;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;

import com.actionbarsherlock.app.ActionBar;
import com.mol.market.demo.ui.CategoryFragment;
import com.mol.market.demo.ui.CategoryWithHeaderFragment;
import com.mol.market.demo.ui.GameFragment;
import com.mol.market.demo.ui.HomeFragmentActivity;
import com.mol.market.demo.widget.BaseFragmentActivity.KeyStore;

/**
 * Author: Andrew.Wang
 * Date  : 13-1-21
 */
public class ThreePagerAdapter extends FragmentStatePagerAdapter implements
        ActionBar.TabListener, ViewPager.OnPageChangeListener {
	
	static final int NUM_ITEMS = 3;

    static class TabInfo {
        private final Class<? extends Fragment> clazz;
        private final CharSequence tag;
        private final Bundle args;

        public TabInfo(Class<? extends Fragment> _clazz, CharSequence _tag, Bundle _args) {
            this.clazz = _clazz;
            this.tag = _tag;
            this.args = _args;
        }
    }

    private Context mContext;
    private Activity mActivity;
    private ViewPager mPager;
    private ActionBar mBar;
    private List<TabInfo> mTabs = new ArrayList<TabInfo>();

    public ThreePagerAdapter(FragmentManager fm, Activity activity, Context context, ViewPager pager, ActionBar bar) {
        super(fm);
        this.mActivity = activity;
        this.mContext = context;
        this.mPager = pager;
        this.mBar = bar;
        this.mPager.setOnPageChangeListener(this);
    }

    public void addTab(ActionBar.Tab tab, Class<? extends Fragment> clazz,
            CharSequence tag, Bundle args) {
        mTabs.add(new TabInfo(clazz, tag, args));
        tab.setTabListener(this);
        mBar.addTab(tab);
    }

    @Override
    public int getCount() {
        return mTabs.size();
    }

    @Override
    public Fragment getItem(int position) {
        TabInfo info = mTabs.get(position);
        if(position == 1 && mActivity instanceof HomeFragmentActivity) {
        	if(((HomeFragmentActivity)mActivity).getGameTabPos() == HomeFragmentActivity.TAB_TYPE) {
        		Bundle args = new Bundle();
    			args.putString(KeyStore.KEY_ARG, KeyStore.TARGET_CATEGORY_GAME);
        		return Fragment.instantiate(mContext, CategoryWithHeaderFragment.class.getName(), args);
        	}
        }
        return Fragment.instantiate(mContext, info.clazz.getName(), info.args);
    }
    
    @Override
	public int getItemPosition(Object object) {
		return ThreePagerAdapter.POSITION_NONE;
	}

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        mBar.selectTab(mBar.getTabAt(position));
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        mPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
    }
}
